package demo1;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {


    public static void main1(String[] args) {
        int[][] arr = new int[3][5];
        System.out.println(arr.length);
    }

    public static void main2(String[] args) {
        int[][] array ={{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        System.out.println(searchMatrix(array,3));
    }

    //搜索二维矩阵
    public static boolean searchMatrix(int[][] matrix, int target) {
        int len1 = matrix.length;
        int len2 = matrix[0].length;
        int l = 0;
        int r = len1-1;
        while (l < r) {
            int mid = l+(r-l+1)/2;
            if (matrix[mid][0] <= target) l = mid;
            else r = mid-1;
        }
        int left=0;int right = len2-1;
        while (left < right) {
            int mid = left+(right-left)/2;
            if(matrix[l][mid]<target) left = mid+1;
            else right = mid;
        }
        if(matrix[l][right]==target) return true;
        else return false;
    }

    //交换后字典集最小的字符串
    public String getSmallestString(String s) {
        char[] arr = s.toCharArray();
        for (int i = 0; i + 1 < arr.length; i++) {
            if (arr[i] > arr[i + 1] && arr[i] % 2 == arr[i + 1] % 2) {
                char temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
                break;
            }
        }
        return new String(arr);
    }


    //寻找峰值
    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            int mid = left+(right-left)/2;
            if(nums[mid]<nums[mid+1]){
                left = mid+1;
            }else right = mid;
        }
        return right;
    }


    //搜索插入位置
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length-1;
        while (left < right) {
            int mid = left+(right-left)/2;
            if (nums[mid] < target) {
                left = mid+1;
            }else right = mid;
        }
        if(target>nums[right]) return right+1;
        return right;
    }

    //山脉数组的峰顶索引
    public int peakIndexInMountainArray(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int mid = left + (right-left)/2;
            if(arr[mid]<arr[mid]){
                left = mid;
            }else if(arr[mid]>arr[mid]){
                right=mid;
            }else return mid;
        }
        return left;
    }
}
